package com.inno.auth.controller.controller;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.base.Preconditions;
import com.inno.auth.common.annotiation.Log;
import com.inno.auth.common.context.LoginUserContext;
import com.inno.auth.common.entity.CommonReturnType;
import com.inno.auth.common.entity.PageResult;
import com.inno.auth.common.enums.FeedbackTypeEnum;
import com.inno.auth.controller.dto.EnumDTO;
import com.inno.auth.controller.dto.FeedbackDTO;
import com.inno.auth.domain.entity.FeedbackBO;
import com.inno.auth.domain.service.FeedbackDomainService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author sean
 */
@RestController
@RequestMapping("/auth/feedback")
public class FeedbackController {
    @Resource
    private FeedbackDomainService feedbackDomainService;

    @GetMapping("/getTypeList")
    public CommonReturnType<List<EnumDTO>> getTypeList() {
        return CommonReturnType.success(
                Arrays.stream(FeedbackTypeEnum.values()).map(typeEnum -> {
                    EnumDTO enumDTO = new EnumDTO();
                    enumDTO.setCode(typeEnum.getCode());
                    enumDTO.setDesc(typeEnum.getDesc());
                    return enumDTO;
                }).toList()
        );
    }

    @PostMapping("/submit")
    @Log(operationType = "提交反馈")
    public CommonReturnType<Void> submit(@RequestBody FeedbackDTO feedbackDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(feedbackDTO.getType()),"请选择反馈类型");
        Preconditions.checkArgument(!StringUtils.isBlank(feedbackDTO.getTitle()),"请填写标题");
        feedbackDTO.setCurrentUser(LoginUserContext.getUserId());
        FeedbackBO feedbackBO = convertDTOToBO(feedbackDTO);
        this.feedbackDomainService.submit(feedbackBO);
        return CommonReturnType.success();
    }

    @PostMapping("/queryPage")
    public CommonReturnType<PageResult<FeedbackDTO>> queryPage(@RequestBody FeedbackDTO feedbackDTO){
        Preconditions.checkNotNull(feedbackDTO.getPage(),"分页参数不能为空");
        Preconditions.checkNotNull(feedbackDTO.getPageSize(),"分页参数不能为空");
        FeedbackBO feedbackBO = convertDTOToBO(feedbackDTO);
        PageResult<FeedbackBO> boResult = this.feedbackDomainService.queryPage(feedbackBO);
        return CommonReturnType.success(PageResult.of(convertBOListToDTOList(boResult.getList()), boResult.getTotal()));
    }

    /**
     * 操作反馈
     */
    @PostMapping("/process")
    public CommonReturnType<Void> process(@RequestBody FeedbackDTO feedbackDTO) {
        Preconditions.checkNotNull(feedbackDTO.getId(),"id不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(feedbackDTO.getStatus()),"请选择处理状态");

        Preconditions.checkArgument(!StringUtils.isBlank(feedbackDTO.getNotifyType()),"请选择通知方式");

        this.feedbackDomainService.processFeedback(convertDTOToBO(feedbackDTO));
        return CommonReturnType.success();

    }


    private FeedbackBO convertDTOToBO(FeedbackDTO feedbackDTO) {
        FeedbackBO feedbackBO = new FeedbackBO();
        BeanUtil.copyProperties(feedbackDTO, feedbackBO);
        return feedbackBO;
    }
    private List<FeedbackDTO> convertBOListToDTOList(List<FeedbackBO> boList) {
        return boList.stream().map(bo -> {
            FeedbackDTO feedbackDTO = new FeedbackDTO();
            BeanUtil.copyProperties(bo, feedbackDTO);
            return feedbackDTO;
        }).toList();
    }
}
